package com.zlm.user.emp.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.base.PageEntity;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.constants.CommonConstants;
import com.zlm.common.dto.userEmp.SysOrgDto;
import com.zlm.common.entity.EmpOrgCollectDO;
import com.zlm.common.enums.CompanyNatureEnum;
import com.zlm.common.enums.CompanyScaleEnum;
import com.zlm.user.emp.dao.EmpOrgCollectDao;
import com.zlm.user.emp.dao.SysOrgDao;
import com.zlm.user.emp.service.EmpOrgCollectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: duyufu
 * @Date: 2019/5/5 13:58
 */
@Service
public class EmpOrgCollectServiceImpl extends AbstractBaseService<EmpOrgCollectDao, EmpOrgCollectDO> implements EmpOrgCollectService {

    @Autowired
    @Override
    public void setBaseDao(EmpOrgCollectDao empOrgCollectDao) {
        this.baseDao = empOrgCollectDao;
    }
    @Autowired
    private SysOrgDao sysOrgDao;

    @Override
    public String saveEmpOrgCollect(Integer operationCode, Integer orgId, Integer empId) {
        if (operationCode.intValue() == 1) {
            //收藏公司
            int count = baseDao.countEmpOrgCollect(orgId,empId);
            if (count > 0) {
                return CodeConstants.CODE_10009;
            }
            baseDao.saveEmpOrgCollect(orgId,empId);
        }else if (operationCode.intValue() == 0) {
            //取消收藏
            baseDao.deleteEmpOrgCollect(orgId,empId);
        }else {
            return CodeConstants.CODE_PARAMS;
        }
        return CodeConstants.CODE_SUCCESS;
    }

    @Override
    public PageEntity queryEmpOrgCollect(Integer empId, Integer pageSize, Integer pageNumber) {
        int count = baseDao.countEmpOrgCollect(null,empId);
        if (null == pageSize || pageSize >= CommonConstants.maxPageSize) {
            pageSize = CommonConstants.pageSize;
        }
        if (null == pageNumber) {
            pageNumber = CommonConstants.pageNumber;
        }else {
            pageNumber = (pageNumber - 1) * pageSize;
        }
        List<SysOrgDto> empOrgCollectList = baseDao.queryEmpOrgCollect(empId,pageSize,pageNumber);
        for (SysOrgDto sysOrgDto : empOrgCollectList) {
            if (StringUtils.isNotBlank(sysOrgDto.getAttUrl())) {
                sysOrgDto.setAttUrl(CommonConstants.IMG_URL.concat(sysOrgDto.getAttUrl()));
            }
            sysOrgDto.setOrgNatureName(CompanyNatureEnum.getNameByCode(sysOrgDto.getOrgNature()));
            sysOrgDto.setOrgScaleName(CompanyScaleEnum.getNameByCode(sysOrgDto.getOrgScale()));
        }
        PageEntity page = getPage(pageNumber,pageSize,count,empOrgCollectList);
        return page;
    }


}
